﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using CMISLibrary.Libraries;
using busDanhMuc;
using EntitiesDanhMuc;
using BaocaoNPC.common;

namespace NPCUS_DanhMuc.Usercontrols
{
    /// <summary>
    /// Interaction logic for QUANLYNGUOIDUNG.xaml
    /// </summary>
    public partial class QUANLYNGUOIDUNG : UserControl
    {
        DataSet dsUserName = new DataSet();
        private string strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqlyDnhap != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqlyDnhap != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqlyDnhap : "PA";
        private string strUser_name = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "OMS";
        string maloi;
        //cls_S_USER_bus cls = new cls_S_USER_bus();
        DataRowView drv = null;
        private string TRANGTHAINUT = "";
        cls_NPC_USER_bus cls = new cls_NPC_USER_bus();             

        public QUANLYNGUOIDUNG()
        {
            InitializeComponent();
            Component.TabIndexManager.RegisterTabManagerWithSelectAll(this);
            set_ChucNang("LOAD");

        }
        private void set_ChucNang(string strChucNang)
        {
            try
            {
                switch (strChucNang)
                {
                    #region FORM LOAD
                    case "LOAD":
                        set_nut_menu(true, true, false, false);
                        lock_text(false, false, false, false, false, false, false);

                        fill_data();
                        break;
                    #endregion
                }
            }
            catch (Exception ex)
            {
                SetStatus("THONGBAOLOI", btnthaydoimatkhau, ex.ToString());
                return;
            }
        }
        private void fill_data()
        {
            dsUserName = new DataSet();
            
            dsUserName = GlobalServices.Service_NPCUS.GET_USERNAME_NGUOIDUNG(strMa_dviqly, strUser_name, ref maloi);
            if (dsUserName != null || dsUserName.Tables.Count > 0)
            {
                DataTable dt = dsUserName.Tables[0] as DataTable;
                txtDviQly.Text = NPCUS_DanhMuc.Pglobal.strTen_dviqlyDnhap;
                txtTenDangNhap.Text = strUser_name;
                txtMatKhauOld.Password = dt.Rows[0]["PASSWORD"].ToString();
                txtMatKhauNew.Password = "";
                txtTenDayDu.Text = dt.Rows[0]["FULLNAME"].ToString();
                txtEmail.Text = dt.Rows[0]["EMAIL"].ToString();
                txtSoDienThoai.Text = dt.Rows[0]["PHONE"].ToString();
            }

        }
        private void set_nut_menu(bool thongtin, bool matkhau, bool huy, bool ghi)
        {
            btnthaydoithongtin.IsEnabled = thongtin;
            btnthaydoimatkhau.IsEnabled = matkhau;
            btnHuy.IsEnabled = huy;
            btnGhi.IsEnabled = ghi;
        }
        private void lock_text(bool DviQly, bool tendangnhap, bool matkhauOld, bool matkhauNew, bool tendaydu, bool email, bool sodienthoai)
        {
            txtDviQly.IsEnabled = DviQly;
            txtTenDangNhap.IsEnabled = tendangnhap;
            txtMatKhauOld.IsEnabled = matkhauOld;
            txtMatKhauNew.IsEnabled = matkhauNew;
            txtTenDayDu.IsEnabled = tendaydu;
            txtEmail.IsEnabled = email;
            txtSoDienThoai.IsEnabled = sodienthoai;
        }

        private void btnthaydoithongtin_Click(object sender, RoutedEventArgs e)
        {
            TRANGTHAINUT = "THONGTIN";
            set_nut_menu(false, false, true, true);
            lock_text(false, false, false, false, true, true, true);
            txtTenDayDu.Focus();
        }

        private void btnthaydoimatkhau_Click(object sender, RoutedEventArgs e)
        {
            TRANGTHAINUT = "PASS";
            set_nut_menu(false, false, true, true);
            lock_text(false, false, true, true, false, false, false);
            txtMatKhauOld.Password = "";
            txtMatKhauOld.Focus();
        }

        private void btnHuy_Click(object sender, RoutedEventArgs e)
        {
            TRANGTHAINUT = "HUY";
            set_nut_menu(true, true, false, false);
            lock_text(false, false, false, false, false, false, false);
            fill_data();
        }

        private void btnGhi_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!check_ghi()) return;
                if (MessageBox.Show("Xác nhận Ghi thay đổi thông tin cho tên đăng nhập: " + txtTenDangNhap.Text.Trim() + " \r\n Nhấn OK     : Tiếp tục Ghi.\r\n Nhấn Cancel: Không thực hiện Ghi.", "Thông báo", MessageBoxButton.OKCancel, MessageBoxImage.Information) == MessageBoxResult.OK)
                {
                    SetStatus("THONGBAOBATDAU", btnGhi, "Đang cập nhật dữ liệu ...");
                    short t = 1;
                    string matkhau = "";
                    if (TRANGTHAINUT == "THONGTIN") matkhau = txtMatKhauOld.Password.Trim();
                    if (TRANGTHAINUT == "PASS") matkhau = NPCUS_DanhMuc.Pglobal.MaHoaMatKhau(txtMatKhauNew.Password.ToString());
                    
                    if (!GlobalServices.Service_NPCUS.update_USERNAME(strMa_dviqly, txtTenDangNhap.Text.Trim(), txtTenDayDu.Text.Trim(), matkhau, txtEmail.Text.Trim(), txtSoDienThoai.Text.Trim(), t, ref maloi))                    
                    {
                        SetStatus("THONGBAOLOI", btnGhi, maloi);
                        return;
                    }
                    SetStatus("THONGBAOKETTHUC", btnGhi, "Cập nhật thành công!");
                    fill_data();
                    set_nut_menu(true, true, false, false);
                    lock_text(false, false, false, false, false, false, false);
                }
            }
            catch (Exception ex)
            { SetStatus("THONGBAOLOI", btnGhi, ex.ToString()); }
        }
        private bool check_ghi()
        {
            DataTable dt = dsUserName.Tables[0] as DataTable;
            if (TRANGTHAINUT == "THONGTIN")
            {
                if (txtTenDayDu.Text.Trim() == "")
                {
                    SetStatus("THONGBAOLOI", btnGhi, "Tên dầy đủ không để trống");
                    txtTenDayDu.Focus();
                    return false;
                }
            }
            if (TRANGTHAINUT == "PASS")
            {
                if (dt.Rows[0]["PASSWORD"].ToString() != NPCUS_DanhMuc.Pglobal.MaHoaMatKhau(txtMatKhauOld.Password.ToString()))
                {
                    SetStatus("THONGBAOLOI", btnGhi, "Mật khẩu cũ không đúng!");
                    txtMatKhauOld.Focus();
                    return false;
                }
                if (txtMatKhauNew.Password.ToString().Trim() == "")
                {
                    SetStatus("THONGBAOLOI", btnGhi, "Mật khẩu mới không để trống");
                    txtMatKhauNew.Focus();
                    return false;
                }
            }
            return true;
        }
        protected void SetStatus(string Begin, Control ControlA, string strNote)
        {
            switch (Begin)
            {
                case "THONGBAOLOI":
                    this.Cursor = null;
                    ControlA.Cursor = null;
                    Component.Validate.ValidationManager.showMessOnView(this, 10000, strNote, Component.Validate.ValidationManager.MESSEGER_TYPE.ERROR_MESSAGE);
                    break;
                case "THONGBAOBATDAU":

                    this.Cursor = Cursors.Wait;
                    ControlA.Cursor = Cursors.Wait;
                    lblstatus.Foreground = Brushes.Blue;
                    lblstatus.Content = strNote;
                    break;
                case "THONGBAOKETTHUC":
                    this.Cursor = null;
                    ControlA.Cursor = null;
                    Component.Validate.ValidationManager.showMessOnView(this, 10000, strNote, Component.Validate.ValidationManager.MESSEGER_TYPE.SUCCESS_MESSAGE);
                    //lblstatus.Foreground = Brushes.Blue;
                    break;
                case "THONGBAONULL":
                    lblstatus.Content = "";
                    this.Cursor = null;
                    ControlA.Cursor = null;
                    break;
                case "THONGBAOCONTRONULL":
                    //lblError.Foreground = Brushes.Blue;
                    //lblError.Content = strNote;
                    this.Cursor = null;
                    ControlA.Cursor = null;
                    // Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
                    break;
            }
        }
        #region Lost_GostFocus
        private void txtMatKhauOld_LostFocus(object sender, RoutedEventArgs e)
        {
            txtMatKhauOld.Background = Brushes.Transparent;
        }

        private void txtMatKhauNew_LostFocus(object sender, RoutedEventArgs e)
        {
            txtMatKhauNew.Background = Brushes.Transparent;
        }

        private void txtTenDayDu_LostFocus(object sender, RoutedEventArgs e)
        {
            txtTenDayDu.Background = Brushes.Transparent;
        }

        private void txtEmail_LostFocus(object sender, RoutedEventArgs e)
        {
            txtEmail.Background = Brushes.Transparent;
        }

        private void txtSoDienThoai_LostFocus(object sender, RoutedEventArgs e)
        {
            txtSoDienThoai.Background = Brushes.Transparent;
        }

        private void txtMatKhauOld_GotFocus(object sender, RoutedEventArgs e)
        {
            txtMatKhauOld.Background = Brushes.Gold;
            txtMatKhauOld.SelectAll();
        }

        private void txtMatKhauNew_GotFocus(object sender, RoutedEventArgs e)
        {
            txtMatKhauNew.Background = Brushes.Gold;
            txtMatKhauNew.SelectAll();
        }

        private void txtTenDayDu_GotFocus(object sender, RoutedEventArgs e)
        {
            txtTenDayDu.Background = Brushes.Gold;
            txtTenDayDu.SelectAll();
        }

        private void txtEmail_GotFocus(object sender, RoutedEventArgs e)
        {
            txtEmail.Background = Brushes.Gold;
            txtEmail.SelectAll();
        }

        private void txtSoDienThoai_GotFocus(object sender, RoutedEventArgs e)
        {
            txtSoDienThoai.Background = Brushes.Gold;
            txtSoDienThoai.SelectAll();
        }

        private void txtMatKhauOld_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                txtMatKhauNew.Focus();
            }
        }

        private void txtMatKhauNew_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnGhi.Focus();
            }
        }
        #endregion
    }
}
